<?php
namespace PsiFramework\Classes\Util\Object;

import("Classes.Model.ModelInterface.IPrimitiveObject");
import("Classes.Util.Database.Query.SqlType");

use \PsiFramework\Classes\Util\Database\Query\SqlType;
use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The Collection class is used to manage Collections
 *
 * @package Util.Object
 */
abstract class Collection implements IPrimitiveObject
{

    protected /*CollectionElement[]*/ $_elements ;

    protected function __construct(/*CollectionElement[]*/ $elements=array())
    {
        $this->_elements = $elements ;
    }

    public function getElements()
    {
        return $this->_elements ;
    }

    public function setElements(/*CollectionElement[]*/ $elements)
    {
        $this->_elements = $elements ;
    }

    public function addElement(CollectionElement $element)
    {
        if (is_null($this->_elements)) {
            $this->_elements = array();
        }
        $nb = count($this->_elements);
        if ($nb>0) {
            $previousElement = $this->_elements[$nb-1] ;
            $previousElement->setNextSibling($element);
            $element->setPreviousSibling($previousElement);
        }
        $this->_elements[] = $element ;
    }

    public function serialize(/*boolean*/ $asText=true)
    {
        $allElements = array();
        if (!is_null($this->_elements)) {
            foreach ($this->_elements as $element) {
                $allElements[] = $element->serialize(false);
            }
        }
        if ($asText) {
            return json_encode($allElements);
        } else {
            return $allElements ;
        }
    }

    public function toString()
    {

    }

    public static function getSqlType()
    {
        return SqlType::$text ;
    }

}
